package org.saxion.aktiehuis.DAO;

import java.util.List;

import org.hibernate.Session;
import org.saxion.aktiehuis.DAO.interfaces.KlantDAO;
import org.saxion.aktiehuis.model.Klant;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

public class KlantMock extends HibernateDaoSupport implements KlantDAO {

//	private SessionFactory sessionFactory;
//	
//
//	public void setSessionFactory(SessionFactory sessionFactory) {
//		this.sessionFactory = sessionFactory;
//	}
	
	/**
	 * 
	 */
	//@Transactional
	public int getKlantId(String klantNaam) throws Exception {
		Session session = getSession(true);
		
		//Klant klantDB = (Klant) session.get(Klant.class, klant.getId());
		
		List<Klant> klanten = session.createQuery("FROM Klant k WHERE k.naam = :naam").setParameter("naam", klantNaam).list(); 
		
		if(klanten.size() == 0){
			return 0;
		}else{
			return klanten.get(0).getId();
		}
		
		
	}
	@Transactional
	public int addKlant(String klantNaam) {
		Klant klant = new Klant();
		klant.setNaam(klantNaam);
		Session session = getSession(true);
		session.save(klant);
		session.flush();
		
		return klant.getId();
	}

}
